package game.UIComponent
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import game.common.CustomBitmap;
	import game.controller.GameDispatcher;
	import game.event.EventName;
	import game.event.GameEvent;
	import game.utils.Utils;

	public class OrderSquare extends UIBase
	{
		private var _square1:CustomBitmap;
		private var _square2:CustomBitmap;
		
		private var _source1:String;
		
		private var _squareBg:CustomBitmap;

		public static const HORIZONTAL:String="horizontal";
		public static const VERTICAL:String="vertical";
		
		private var _firstNum:TextField;
		private var _secondNum:TextField;
		
		private var _type:String;
		
		private var _closeBtn:CustomBitmap;
		
		private var _dispatcher:GameDispatcher=GameDispatcher.getInstance();

		/**
		 * @dir horizontal-横放的	vertical=竖放的
		 * @square1 第一个方格的图片连接
		 * @square2 第二个方格的图片连接
		 * @event1	第一个方格点击派发事件名
		 * @event2	第二个方格点击派发事件名
		 * @close	丢弃按钮点击派发事件名
		 * */
		public function OrderSquare(type:String, square1:String, square2:String="openIcon")
		{
			_source1=square1;
			_type=type;
			
			_squareBg=new CustomBitmap();
			addChild(_squareBg);
			
			_square1=new CustomBitmap();
			_square1.buttonMode=true;
			_square1.name="square1";
			_square1.x=_squareBg.x + 3;
			_square1.y=_squareBg.y + 3;
			_square1.source=square1;
			_square1.addEventListener(MouseEvent.CLICK, onClick);
			addChild(_square1);
			
			_firstNum=Utils.getCommonTextField("1500"+"份",14,_square1.x + 2,_square1.y + 40);
			_firstNum.visible=false;
			addChild(_firstNum);			
			
			_square2=new CustomBitmap();
			_square2.name="square2";
			_square2.buttonMode=true;
			_square2.source=square2;
			_square2.addEventListener(MouseEvent.CLICK, onClick);
			addChild(_square2);
			
//			_firstNum=Utils.getCommonTextField("1500",16,_square1.x + 2,_square1.y + 40);
//			_firstNum.visible=false;
//			addChild(_firstNum);
			
			_closeBtn=new CustomBitmap();
			_closeBtn.name="closeBtn";
			_closeBtn.source="menuClose";
			_closeBtn.x=_square1.x + _square1.width - _closeBtn.width;
			_closeBtn.y=_square1.y;
			_closeBtn.visible=false;
			_closeBtn.addEventListener(MouseEvent.CLICK,onClick);
			addChild(_closeBtn);
			
			if (type == "horizontal")
			{
				initHorizontal();
			}
			else if (type == "vertical")
			{
				initVertical();
			}
			else
				trace("方向只接受横或竖");
		}

		private function initHorizontal():void
		{
			_squareBg.source="menuSquareBg";

			_square2.x=_square1.x - 3;
			_square2.y=_square1.y + _square1.height + 3;
			
			_square2.width=46;
			_square2.height=47;
			
			

		}

		private function initVertical():void
		{
			_squareBg.source="menuSelectBg";

			_square2.x=_square1.x + _square1.width + 13;
			_square2.y=_square1.y;

		}

		private function onClick(e:MouseEvent):void
		{
			_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_MENU));		
		}
		
		public function set firstFoodType(source:String):void
		{
			_square1.source = source;
			if (_type == VERTICAL) 
			{
				_square1.width=50;
				_square1.height=50;
				_closeBtn.visible = true;
			}
			else{
				_square1.width=40;
				_square1.height=48;				
				
			}				
		}
		
		public function setEmptyFoodType():void
		{			
			_square1.source = _source1;
			
			if (_type == VERTICAL) 
			{			
				_square1.width=50;
				_square1.height=50;
				_closeBtn.visible = true;
			}
			else{
				_square1.width=52;
				_square1.height=56;								
			}				

		}
		
		public function set secondFoodType(source:String):void
		{
			_square2.source = source;
			if (_type == VERTICAL) 
			{
				_square2.width=50;
				_square2.height=50;
			}
			else{
				_square2.width=46;
				_square2.height=47;
				//_closeBtn.visible = true;
			}			
		}
		
		
		public function set close(param:Boolean):void
		{
			_closeBtn.visible = param;
			
		}
		
		public function removeAllEvent():void
		{
			_closeBtn.removeEventListener(MouseEvent.CLICK,onClick);
			_square1.removeEventListener(MouseEvent.CLICK,onClick);
			_square2.removeEventListener(MouseEvent.CLICK,onClick);
		}

		public function set foodNum(value:int):void
		{
			if (value == 0) 
			{
				_firstNum.visible = false;
			}else{
				_firstNum.visible = true;
			}
			_firstNum.text = value.toString()+"份";
		}
		
		private var _reg:RegExp=/^\d*/;
		
		public function get foodNum():int
		{
			//return int(_firstNum.text.replace(_reg,""));
			return int(_firstNum.text.match(_reg));
		}

		public function get source1():String
		{
			return _square1.source;
		}

	}


}